package org.fly.controller.wap.customer;

import org.fly.bean.Customer;
import org.fly.bean.OrderProduct;
import org.fly.bean.Page;
import org.fly.bean.query.OrderProductQuery;
import org.fly.bean.query.OrderQuery;
import org.fly.bean.warpper.OrderWarpper;
import org.fly.controller.AbstractController;
import org.fly.service.OrderService;
import org.fly.utils.JsonUtils;
import org.fly.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by rongzhao.zhu on 2018/4/9.
 */
@Controller
@RequestMapping("/wap/customer/order/")
public class WapCOrderController extends AbstractController {


    @Autowired
    OrderService orderService;

    @RequestMapping(value = "add", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String add(OrderProduct orderProduct, HttpServletRequest request){
        try {
            Customer customer = (Customer) UserContext.getUser();
            String customerId = customer.getId();
            orderService.createByProduct(orderProduct,customerId);
            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }
    @RequestMapping(value = "addByCartIdList", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String addByCartIdList(@RequestBody List<String> idList, HttpServletRequest request){
        try {
            Customer customer = (Customer) UserContext.getUser();
            String customerId = customer.getId();
            orderService.createByCart(idList,customerId);
            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }
    @RequestMapping(value = "queryPage", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryPage(OrderQuery query){
        Customer customer = (Customer) UserContext.getUser();
        String customerId = customer.getId();
        query.setCustomerId(customerId);
        List<OrderWarpper> list = orderService.listWarpper(query );
        long count = this.orderService.count(query);
        Page page = new Page(query.getPage(),query.getSize());
        page.setData(list);
        page.setTotal(count);
        return JsonUtils.encode(createDataTip(page));
    }


}
